package com.netease.financial.module.browser;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;

import com.netease.financial.R;
import com.netease.financial.common.util.NfsLog;
import com.netease.financial.internal.di.HasComponent;
import com.netease.financial.internal.di.components.BrowserComponent;
import com.netease.financial.internal.di.components.DaggerBrowserComponent;
import com.netease.financial.internal.di.modules.ActivityModule;
import com.netease.financial.internal.di.modules.BrowserModule;
import com.netease.financial.module.main.MainActivity;
import com.netease.financial.module.web.WebViewTabFragment;
import com.netease.financial.ui.activity.BaseAppCompatActivity;

import javax.inject.Inject;

import butterknife.ButterKnife;

public class MainBrowserActivity extends BaseAppCompatActivity implements BrowserView, WebViewTabFragment.OnFragmentInteractionListener, HasComponent<BrowserComponent> {

    private static final String TAG = MainBrowserActivity.class.getSimpleName();

    private BrowserComponent mComponent;
    private BrowserFragment browserFragment;
    public final static String EXTRA_BROWSER_URL = "browser_url";

    private MainBrowserActivity mContext;

    @Inject
    BrowserPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_main_browser);
        ButterKnife.bind(this);

        initialize();
    }

    private void initialize() {
//        browserFragment = new BrowserFragment();
        browserFragment = (BrowserFragment) Fragment.instantiate(this, BrowserFragment.class.getName(), null);
        browserFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_view, browserFragment).commit();
        initializeInjector();
        initializePresenter();
        initializeActionbar();
    }

    private void initializeInjector() {
        mComponent = DaggerBrowserComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .browserModule(new BrowserModule())
                .build();
        mComponent.inject(this);
    }

    private void initializePresenter() {
        mPresenter.setView(this);
        mPresenter.initialize();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.onResume();
    }

    @Override
    protected void onPause() {
        mPresenter.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        mPresenter.onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (browserFragment.getWebView().canGoBack())
                return super.onKeyDown(keyCode, event);
            else {
                Intent intent = new Intent(mContext, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                return true;
            }
        }

        return super.onKeyDown(keyCode, event);
    }

//    @Override
//    public void onBackPressed() {
//        if (browserFragment.getWebView().canGoBack())
//            super.onBackPressed();
//        else {
//            Intent intent = new Intent(this, MainActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//            startActivity(intent);
//        }
//    }

    private void initializeActionbar() {
        enableLeftNavigationItem(true);
        getCloseNavigationItem().setVisibility(View.VISIBLE);
        getCloseNavigationItem().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
            }
        });
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onSuccess() {

    }

    @Override
    public void onError() {

    }

    @Override
    public void setNavigationTitle(String title) {

    }

    @Override
    public void showBottomTabWidget(boolean show) {

    }

    @Override
    public void hideNavBackItem() {

    }

    @Override
    public void showNavBackItem() {

    }

    @Override
    public void hideOptionMenu() {

    }

    @Override
    public void showOptionMenu() {

    }

    @Override
    public void setCurrentTab(int position) {

    }

    @Override
    public BrowserComponent getComponent() {
        return this.mComponent;
    }
}
